package cn.com.utt.unms.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import cn.com.utt.unms.pojo.Page;
@Mapper
public interface AuthPageMapper {
	@Select({"<script>",
	    "SELECT COUNT(*) AS total", 
	    "FROM page",
	    "WHERE user_id=#{userId}",
	    "and (tm_id=10000001 or tm_id=10000003 or tm_id &lt; 10000000)",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	    "</script>"})
	int countByCondition(Map<String, Object> conditions);
	
	
	@Select({"<script>",
	    "SELECT *", 
	    "FROM page ",
	    "WHERE user_id=#{userId}",
	    " and (tm_id=10000001 OR tm_id=10000003 OR tm_id &lt; 10000000) ",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	      "<if test='orderColumn != null'>",
	      "order by ${orderColumn} ${orderDirection}",
	      "</if>",
	      "limit #{start},#{rows}",
	    "</script>"})
	List<Page> queryByCondition(Map<String, Object> conditions);
	// user.page_id
//	@Select("SELECT page.* FROM page,user WHERE 0 = page.id AND user.id=#{userId}")
//	Page queryPubPageByUserId(@Param("userId") Long userId);
	
	@Insert("insert into page(pageList_id,tm_id,path) values(#{pageListId},#{tmId},#{path}) ")
	@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
	void save(Page page);
	@Select("select * from page where path=#{path}")
	Page queryPageByPath(@Param("path") String path);
	
	@Select("select * from page where id=#{id}")
	Page queryPageById(@Param("id") Long id);
	
	@Delete("delete from page where id=#{id}")
	void delPage(@Param("id") Long id);
	
	@Select({"<script>",
	    "select * ", 
	    "FROM page", 
	    "WHERE",
	    "id IN", 
	      "<foreach item='item' index='index' collection='ids'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	List<Page> queryPageByIds(@Param("ids") List<Long> ids);
	
	@Select({"<script>",
	    "select * ", 
	    "FROM page",
	    "WHERE from_agent is not null and from_agent>0 and",  
	    "id IN", 
	      "<foreach item='item' index='index' collection='ids'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	List<Page> queryPageByIdsByFromAgent(@Param("ids") List<Long> ids);
	
	@Select({"<script>",
	    "select * ", 
	    "FROM page", 
	    "WHERE",
	    "pageList_id IN", 
	      "<foreach item='item' index='index' collection='ids'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	List<Page> queryPageByPageListId(@Param("ids") List<Long> ids);
	

}
